package net.orange.console.controller;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import net.orange.architecture.web.Web;
import net.orange.console.SystemConsole;

/**
 * The script console managed bean using in JSF MVC modal. it is include the web
 * page content and control the action and page forward direction in the web
 * site.
 */
@ManagedBean(name="console")
@SessionScoped
public class SystemConsoleController {
	/**
	 * The program script
	 */
	private String script;

	/**
	 * The result of program.
	 */
	private String result;

	/**
	 * @return Returns the result.
	 */
	public String getResult() {
		return result;
	}

	/**
	 * @param result
	 *            The result to set.
	 */
	public void setResult(String result) {
		this.result = result;
	}

	/**
	 * @return Returns the script.
	 */
	public String getScript() {
		return script;
	}

	/**
	 * @param script
	 *            The script to set.
	 */
	public void setScript(String script) {
		this.script = script;
	}

	/**
	 * Execute the script and return result to web page.
	 * 
	 * @return The JSF form outcome. "execute" for ok,throw exception else.
	 */
	public String execute() throws Exception {
		SystemConsole bus = new SystemConsole();
		try {
			Object r = bus.eval(script);
			result = String.valueOf(r);
		} catch (Exception e) {
			result = e.getMessage();
		}

		return Web.JSF_NAVIGATION_CONSOLE_EXECUTE;
	}
}
